﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using University.Workshop.DataAccess;
using University.Workshop.Model;
using System.Globalization;

namespace University.Workshop.ClientApp
{
    /// <summary>
    /// Lógica de interacción para IllegalBetting.xaml
    /// </summary>
    public partial class IllegalBetting : Window
    {
        User user = new User();
        Client client = new Client();
        string[] players;
        int number;
        long betting;
        public IllegalBetting()
        {
            InitializeComponent();
        }

        public IllegalBetting(User user, string[] players)
        {
            InitializeComponent();
            this.user = user;
            this.players = players;
            number = players.Length;
            setOptions();
        }

        public void setOptions()
        {
            if (number > 0)
            {
                player1.Content = players[0];
                p1.IsEnabled = true;
                if (number > 1)
                {
                    player2.Content = players[1];
                    p2.IsEnabled = true;
                    if (number > 2)
                    {
                        player3.Content = players[2];
                        p3.IsEnabled = true;
                        if (number > 3)
                        {
                            player4.Content = players[3];
                            p4.IsEnabled = true;
                            if (number > 4)
                            {
                                player5.Content = players[0];
                                p5.IsEnabled = true;
                            }
                        }
                    }
                }
            }
        }

        public void finishedHand(string winner)
        {
            int index = search(0, winner);
            long money=0;
            if (players[index] == winner)
            {
                switch (index)
                {
                    case 0:
                        money = Convert.ToInt64(bet1.Content, CultureInfo.InvariantCulture);
                        break;
                    case 1:
                        money = Convert.ToInt64(bet1.Content, CultureInfo.InvariantCulture);
                        break;
                    case 2:
                        money = Convert.ToInt64(bet1.Content, CultureInfo.InvariantCulture);
                        break;
                    case 3:
                        money = Convert.ToInt64(bet1.Content, CultureInfo.InvariantCulture);
                        break;
                    case 4:
                        money = Convert.ToInt64(bet1.Content, CultureInfo.InvariantCulture);
                        break;
                }
                user.Chips = user.Chips + (money * 2);
                chips.Content = Convert.ToString(user.Chips, CultureInfo.InvariantCulture);
                clean();
            }
        }

        public void clean()
        {
            bet1.Content = "0";
            bet2.Content = "0";
            bet3.Content = "0";
            bet4.Content = "0";
            bet5.Content = "0";
        }

        public int search(int index, string name)
        {
            while ((index < number) && (players[index] == name))
            {
                index++;
            }
            return index;
        }
        public void deletePlayer(string name)
        {
            int index=search(0,name);
            long money=0;
            string no = "No player";
            if (players[index] == name)
            {
                players[index] = no;
                switch (index)
                {
                    case 0:
                        player1.Content = no;
                        p1.IsEnabled = false;
                        money = Convert.ToInt64(bet1.Content, CultureInfo.InvariantCulture);
                        break;
                    case 1:
                        player2.Content = no;
                        p2.IsEnabled = false;
                        money = Convert.ToInt64(bet2.Content, CultureInfo.InvariantCulture);
                        break;
                    case 2:
                        player3.Content = no;
                        p3.IsEnabled = false;
                        money = Convert.ToInt64(bet3.Content, CultureInfo.InvariantCulture);
                        break;
                    case 3:
                        player4.Content = no;
                        p4.IsEnabled = false;
                        money = Convert.ToInt64(bet4.Content, CultureInfo.InvariantCulture);
                        break;
                    case 4:
                        player5.Content = no;
                        p5.IsEnabled = false;
                        money = Convert.ToInt64(bet5.Content, CultureInfo.InvariantCulture);
                        break;
                }
                user.Chips = user.Chips + money;
                chips.Content = Convert.ToString(user.Chips, CultureInfo.InvariantCulture);
            }
        }

        private void amount_TextChanged(object sender, TextChangedEventArgs e)
        {
            string amountBet = amount.Text;
            int size = amountBet.Length, index=0;
            while ((amountBet[index] < 58) && (amountBet[index] > 47))
            {
                index++;
            }
            if ((size == index + 1) && (!String.IsNullOrEmpty(amountBet)))
            {
                if (betting <= user.Chips)
                {
                    betting = Convert.ToInt64(amountBet,CultureInfo.InvariantCulture);
                    bet.IsEnabled = true;
                }
                else
                {
                    message.Content = "Insufficient chips";
                    bet.IsEnabled = false;
                }
            }
            else
            {
                message.Content = "Insert only numbers";
                bet.IsEnabled = false;
            }
        }

        private void bet_Click(object sender, RoutedEventArgs e)
        {
            if (changeAmount())
            {
                user.Chips = user.Chips - betting;
                chips.Content = Convert.ToString(user.Chips, CultureInfo.InvariantCulture);
            }
        }

        private bool changeAmount()
        {
            if (p1.IsChecked == true)
                bet1.Content = Convert.ToString(Convert.ToInt64(bet1.Content, CultureInfo.InvariantCulture) + betting,CultureInfo.InvariantCulture);
            else
            {
                if (p2.IsChecked == true)
                    bet2.Content = Convert.ToString(Convert.ToInt64(bet2.Content, CultureInfo.InvariantCulture) + betting, CultureInfo.InvariantCulture);
                else
                {
                    if (p3.IsChecked == true)
                        bet3.Content = Convert.ToString(Convert.ToInt64(bet3.Content, CultureInfo.InvariantCulture) + betting, CultureInfo.InvariantCulture);
                    else
                    {
                        if (p4.IsChecked == true)
                            bet4.Content = Convert.ToString(Convert.ToInt64(bet4.Content, CultureInfo.InvariantCulture) + betting, CultureInfo.InvariantCulture);
                        else
                        {
                            if (p1.IsChecked == true)
                                bet5.Content = Convert.ToString(Convert.ToInt64(bet5.Content, CultureInfo.InvariantCulture) + betting, CultureInfo.InvariantCulture);
                            else
                                return false;
                        }
                    }
                }
            }
            return true;
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            MessageBoxResult quitMessage = MessageBox.Show("Are you sure you wanna quit?", "Warning!!!", MessageBoxButton.YesNo);
            if (quitMessage == MessageBoxResult.Yes)
            {
                client.Send("14 " + user.Name + " " + user.Chips);
                client.Receive();
            }
            else
                e.Cancel = true;
        }
    }
}
